<script setup>
import { defineOptions }  from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useregister, userlogin } from '@/api/user';
import instance from '../utilis/requet'
import { watch } from 'vue';
import { userstore } from '@/stores/userstore';
import { useRouter } from 'vue-router';
const isRegister = ref(true)
const tokenstore=userstore()
const router=useRouter()
const formodal=ref({
  username: 'houhou123',
  password: 'qe8059696',
  repassword: 'qe8059696'
})
const rules={
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是 5-10 位 的字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rules, value, callback) => {
        // 判断 value 和 当前 form 中收集的 password 是否一致
        if (value !== formodal.value.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback() //就算校验成功，也需要callback
        }
      },
      trigger: 'blur'
    }
  ]
}
const form=ref('')
const register=async()=>{
  // console.log(form);
  await form.value.validate()
  const res= await useregister(formodal.value)
  console.log(res.data.message);
  // instance({
  //   url:'/api/reg',
  //   method:'post',
  //   data:{
  //     ...formodal.value
  //   }
  // }).then(res=>{
  //   console.log(res);
  // })
  ElMessage({
    showClose: true,
    message: `${res.data.message}`,
    type: 'success',
    showClose:false
  })
  // 切換
  isRegister.value=false
}
watch(isRegister,()=>{
  formodal.value={
    username: 'houhou123',
  password: 'qe8059696',
  repassword: ''
  }
})
defineOptions({
   name:'login'
})
const login=async()=>{
await form.value.validate()
const res= await userlogin(formodal.value)
console.log(res);
tokenstore.token(res.data.token)
ElMessage({
    showClose: true,
    message: `${res.data.message}`,
    type: 'success',
    showClose:false
  })
  router.push('/')
}
</script>

<template>
 <div>
   
   <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 注册相关表单 -->
      <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="formodal" :rules="rules">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item> 
        <el-form-item prop="username">
          <el-input v-model="formodal.username" :prefix-icon="User" placeholder="请输入用户名" ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="formodal.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
            v-model="formodal.repassword"
          ></el-input>
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="register">
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 登陆相关表单 -->
      <el-form ref="form" size="large" autocomplete="off" :model="formodal" :rules="rules" v-else>
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formodal.username"></el-input>
        </el-form-item>
        <el-form-item prop="password"> 
          <el-input
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="formodal.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space  @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
 </div>

</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background:
      url('../assets/1111111.png') no-repeat 60% center / 140px auto,
      url('../assets/10006.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>

